package  com.yuanqi.dz.dc.util;
import java.util.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.TimeZone;

public class DateUtil {

	
	//比较2个时间是否相差15分钟
	public static boolean compareDate15(Date start,Date end){
		boolean flag = false;
		if(start==null || end==null){
			flag = false ;
		}else{
			Date d = DateUtil.datePlusMinute_returnDate(start, 13);
			if(d.getTime()<end.getTime()){
				flag = true ;
			}
		}
		return flag ;
	}
	public static Date datePlusMinute_returnDate(Date date,int m){
		Date d = null;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, m);
		d = c.getTime();

		return d;
	}

	public static Timestamp getTimeStamp(String time,String format){
		if(time == null || "".equals(time))
			return null;
		Timestamp ts = null;
		ts = new Timestamp(stringToDate(time, format).getTime());
		return ts ;
	}
	public static String datePlusMinute(Date date,int m){
		String s = "";
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, m);
		Date d = c.getTime();
		s = dateToString(d, "HH:mm");
		return s;
	}

	public static String datePlusMinute(Date date, int m, String format){
		String s = "";
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, m);
		Date d = c.getTime();
		s = dateToString(d, format);
		return s;
	}





	public static Date stringToDate(String time,String format) {

		SimpleDateFormat simple = new SimpleDateFormat(format);

		Date d=null;
		try {
			d = simple.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return d ;
	}

	public static String dateToString(Date date,String format){
		if(date==null){
			return null;
		}
		SimpleDateFormat simple = new SimpleDateFormat(format);
		String time = simple.format(date);
		return time;
	}


	public static String getTimeString(String date) throws Exception{
		Date d= stringToDate(date, "yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat df= new SimpleDateFormat("HH:mm:ss");
		String time= df.format(d);
		return time;

	}

	public static Date datePlusDay(Date date,int num){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.DAY_OF_YEAR, num);
		return c.getTime();
	}

	public static Date getTodayNowEnd(){
		Date toaydnowend = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
			String s = sdf.format(new Date());
			s += " 23:59:59";
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			toaydnowend = sdf2.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return toaydnowend;
	}

	public static java.sql.Timestamp getTodayNowEnd(String date){
		Timestamp ts = null;
		try {
			date += " 23:59:59";
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			Date toaydnowend = sdf.parse(date);
			ts = new Timestamp(toaydnowend.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ts;
	}

	public static java.sql.Timestamp getTodayNowBegin(String date){
		Timestamp ts = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date toaydnowend = sdf.parse(date);
			ts = new Timestamp(toaydnowend.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ts;
	}

	public static java.sql.Timestamp dateToTimestamp(Date date2){
		String Format="yyyy-MM-dd HH:mm:ss";
		Timestamp ts = null;
		try{
			SimpleDateFormat sdf = new SimpleDateFormat(Format);
			Date  date=sdf.parse(dateToString(date2, Format));
			ts=new Timestamp(date.getTime());
		}catch (ParseException e) {
			e.printStackTrace();
		}
		return ts;



	};

	public static String getWeekDate(String strdate){
		String s = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		try {
			Date date = sdf.parse(strdate);
			SimpleDateFormat sdf2 = new SimpleDateFormat("MM/dd EEE");
			s = sdf2.format(date).replace("星期", "周");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return s;
	}

	public static String getScheduleDateWeek(String strdate){
		String s = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		try {
			Date date = sdf.parse(strdate);
			SimpleDateFormat sdf2 = new SimpleDateFormat("EEE");
			s = sdf2.format(date).replace("星期", "周");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return s;
	}

	public static String getScheduleDateDay(String strdate){
		String s = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		try {
			Date date = sdf.parse(strdate);
			SimpleDateFormat sdf2 = new SimpleDateFormat("dd");
			s = sdf2.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return s;
	}

	public static String getScheduleDate(String strdate){
		String s = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		try {
			Date date = sdf.parse(strdate);
			SimpleDateFormat sdf2 = new SimpleDateFormat("EEE MM月dd日");
			s = sdf2.format(date).replace("星期", "周");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return s;
	}

	public static String getDateAfter(String queue){
		return getDateAfter(new java.util.Date(), queue);
	}

	public static String getDateAfter(Date d,String queue){
		return getDateAfter(d, "yyyy-MM-dd", queue);
	}

	public static String getDateAfter(Date d, String dateformat, String queue){
		if(null==queue || "".equals(queue)){
			queue = "0";
		}

		String changetime = "";
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(d);
			now.set(Calendar.DATE, now.get(Calendar.DATE)+Integer.parseInt(queue));

			java.util.Date date = now.getTime();

			SimpleDateFormat sdf = new SimpleDateFormat(dateformat);
			changetime = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return changetime;
	}

	public static int compareDate(Date d1,Date d2){
		if(d1==null || d2==null){
			return 0;
		}
		long l1 = d1.getTime();
		long l2 = d2.getTime();
		if(l1>l2){
			return -1;
		}else if(l1<l2){
			return 1;
		}else {
			return  0;
		}
	}

	/** 
	 * 根据日期获得所在周的日期  
	 * @param mdate 
	 * @return 
	 */  
	@SuppressWarnings("deprecation")  
	public static List<Date> dateToWeek(Date mdate) {  
		int b = mdate.getDay();  
		Date fdate;  
		List<Date> list = new ArrayList<Date>();  
		Long fTime = mdate.getTime() - b * 24 * 3600000;  
		for (int a = 1; a <= 7; a++) {  
			fdate = new Date();  
			fdate.setTime(fTime + (a * 24 * 3600000));  
			list.add(a-1, fdate);  
		}  
		return list;  
	}  
	/**
	 * 获取日期MM+周几
	 * @param count
	 * @return
	 */
	public static String getCurrDateWeek(int count) {  
		Calendar strDate = Calendar.getInstance();  
		strDate.add(Calendar.DATE, count);  
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd EEE");  
		return sdf.format(strDate.getTime());  
	}

	public static String getCurrentDate(int i){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar strDate = Calendar.getInstance();  
		strDate.add(Calendar.DATE, i);  
		return sdf.format(strDate.getTime());  
	}

	/**
	 * 比较两个时间间隔差
	 * @param time1 开始时间
	 * @param time2 结束时间
	 * @param field Calendar类型(Calendar.HOUR,Calendar.MINUTE,Calendar.SECOND,)
	 * @return 时间差
	 * @throws Exception
	 */
	public static int fieldDifference(long time1, long time2, int field) throws Exception{
		if (time1 == time2) {
			return 0;
		} else if (time1 > time2) {
			// 确保time1比time2小
			//			return fieldDifference(time2, time1, field);
			throw new RuntimeException("Set Start date > End date");
		}
		// 下面清除不要参与比较的内容
		Calendar cal1 = Calendar.getInstance();
		cal1.setLenient(false);
		cal1.setTimeInMillis(time1);
		Calendar cal2 = Calendar.getInstance();
		cal2.setLenient(false);
		cal2.setTimeInMillis(time2);
		for (int x = 0; x < Calendar.FIELD_COUNT; x++) {
			if (x > field) {
				cal1.clear(x);
				cal2.clear(x);
			}
		}
		// 重新设定初始值
		time1 = cal1.getTimeInMillis();
		time2 = cal2.getTimeInMillis();
		long ms = 0;
		int min = 0;// 下限,从0开始
		int max = 1;// 每次所加的值,第一次加1
		while (true) {
			// 因为max的值都是相对time1而言,故每次都是从time1开始而不是ms
			cal1.setTimeInMillis(time1);
			cal1.add(field, max);// 将field对应的字段加上max的值
			ms = cal1.getTimeInMillis();
			if (ms == time2) {
				// 两个时间之间相差的值为max
				min = max;
				break;
			} else if (ms > time2) {
				// 超过后,则差值肯定小于max
				break;
			} else {
				// 仍然小于time2,继续增大max,直到ms>=time2为止
				max <<= 1;
			}
		}
		// 上面的操作中没有找到准确的值,接下来使用二分查找以准确找出差值
		while (max > min) {
			cal1.setTimeInMillis(time1);
			int t = (min + max) >>> 1;
			cal1.add(field, t);
			ms = cal1.getTimeInMillis();
			if (ms == time2) {
				min = t;
				break;
			} else if (ms > time2) {
				max = t;
			} else {
				min = t;
			}
		}
		return min;
	}

	public static String getWeekDateText(String strdate){
		String s = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
		try {
			Date date = sdf.parse(strdate);
			SimpleDateFormat sdf2 = new SimpleDateFormat("EEE");
			s = sdf2.format(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return s;
	}

	public static String getDateTimeInterval(String date, String format, String toformat) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat(format);  
		Date todate = sdf.parse(date);

		SimpleDateFormat sdf2 = new SimpleDateFormat(toformat);

		return sdf2.format(todate);
	}





	public static String getCurrentDateYYYYMMDDHHMMSS() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Calendar strDate = Calendar.getInstance();
		return sdf.format(strDate.getTime());
	}
	public static String getCurrentMinu(Date date, int i){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, i);  
		return sdf.format(c.getTime());  
	}

	public static String getCurrentMinuYYYYMMddHHmmss(Date date, int i){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(Calendar.MINUTE, i);  
		return sdf.format(c.getTime());  
	}

	public static String getIntervalTime(String sbeginDate, String sendDate) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date begintime = sdf.parse(sbeginDate);
		Date endtime = sdf.parse(sendDate);

		long intime = endtime.getTime() - begintime.getTime();

		return String.valueOf(intime/(1000*60));
	}

	public static int getIntervalSecondTime(String sbeginDate, String sendDate) throws Exception{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		Date begintime = sdf.parse(sbeginDate);
		Date endtime = sdf.parse(sendDate);

		long intime = endtime.getTime() - begintime.getTime();

		return Integer.parseInt(String.valueOf(intime/(1000)));
	}
	//
	public static long getCountTime(String register_date)throws Exception{
		Date date = stringToDate(register_date,"yyyy-MM-dd HH:mm:ss");
		Date nowdate = new Date();
		long count = date.getTime()+15*60*1000-nowdate.getTime();
		if(count<0) return 0;

		return count/1000;
	}

	/**
	07.
	 * 将毫秒转换为年月日时分秒
	08.
	 *
	09.
	 * @author GaoHuanjie
	10.
	 */
	public static String getYearMonthDayHourMinuteSecond(long timeMillis) {
		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8 beijing")); 
		calendar.setTimeInMillis(timeMillis);
		int year=calendar.get(Calendar.YEAR);

		int month=calendar.get(Calendar.MONTH) + 1;
		String mToMonth=null;
		if (String.valueOf(month).length()==1) {
			mToMonth="0"+month;
		} else {
			mToMonth=String.valueOf(month);
		}
		int day=calendar.get(Calendar.DAY_OF_MONTH);
		String dToDay=null;
		if (String.valueOf(day).length()==1) {
			dToDay="0"+day;
		} else {
			dToDay=String.valueOf(day);
		}
		int hour=calendar.get(Calendar.HOUR_OF_DAY);
		String hToHour=null;
		if (String.valueOf(hour).length()==1) {
			hToHour="0"+hour;
		} else {
			hToHour=String.valueOf(hour);
		}
		int minute=calendar.get(Calendar.MINUTE);
		String mToMinute=null;
		if (String.valueOf(minute).length()==1) {
			mToMinute="0"+minute;
		} else {
			mToMinute=String.valueOf(minute);
		}
		int second=calendar.get(Calendar.SECOND);
		String sToSecond=null;
		if (String.valueOf(second).length()==1) {
			sToSecond="0"+second;
		} else {
			sToSecond=String.valueOf(second);
		}
		return  year+ "-" +mToMonth+ "-" +dToDay+ " "+hToHour+ ":" +mToMinute+ ":" +sToSecond;     
	}
	public static void main(String[] args) {
		//		 time = time.substring(0, 4)+"-"+time.substring(4, 6)+"-"+time.substring(6, 8)+" "+time.substring(8, 10)+":"+time.substring(10, 12)+":"+time.substring(12, 14);
		//System.out.println(getDateAfter(new Date(), "-6")+ " 00:00:00");
		//System.out.println(getDateAfter(new Date(), "0")+ " 23:59:59");
		 String s = dateToString(
		 new Date(1519833600000L),  "yyyy-MM-dd HH:mm:ss");
		 System.out.println(s);
	}

	//yyyyMMddHHmmss转成字符
	public static String timeToString(String time){
		time = time.substring(0, 4)+"-"+time.substring(4, 6)+"-"+time.substring(6, 8)+" "+time.substring(8, 10)+":"+time.substring(10, 12)+":"+time.substring(12, 14);
		return time;
	}



	/**
	 * 根据起始时间获取期间的日期
	 * @param type    类型: 1:yyyy-MM-dd 2:yyyy-MM 3:yyyy
	 * @param start   开始时间
	 * @param end     结束时间
	 * @return        开始到结束时间的list
	 * @throws ParseException 
	 */
	public static List<String> getXLine(String type,String start,String end) throws ParseException{
		List<String> str = new ArrayList<String>();
		SimpleDateFormat sdf = null;
		long par = 0 ;
		long num=0;
		Calendar cal=Calendar.getInstance();
		if(type.equals("1")){
			sdf = new SimpleDateFormat("yyyy-MM-dd");
			par = 3600L*24L*1000L;
			Date d1 = sdf.parse(start);
			Date d2 = sdf.parse(end);
			cal.setTime(d1);
			num = (d2.getTime()-d1.getTime())/par;
		}else if(type.equals("2")){
			sdf = new SimpleDateFormat("yyyy-MM");
			Date d1 = sdf.parse(start);
			cal.setTime(d1);
			long sYear=Integer.parseInt(start.substring(0,4));
			long eYear=Integer.parseInt(end.substring(0,4));
			long sMonth=Integer.parseInt(start.substring(5,7));
			long eMonth=Integer.parseInt(end.substring(5,7));
			num = (eYear-sYear)*12+(eMonth-sMonth);
		}else if(type.equals("3")){
			sdf = new SimpleDateFormat("yyyy");
			par = 3600L*24L*1000L*30L*12L;
			Date d1 = sdf.parse(start);
			Date d2 = sdf.parse(end);
			cal.setTime(d1);
			num = (d2.getTime()-d1.getTime())/par;
		}
		for(int i=0;i<=num;i++){
			if(type.equals("1")){
				if(i==0){
					cal.add(Calendar.DAY_OF_YEAR, 0);
				}else{
					cal.add(Calendar.DAY_OF_YEAR, 1);
				}
				Date dt1=cal.getTime();
				String reStr=sdf.format(dt1);
				str.add(reStr);
			}else if(type.equals("2")){
				if(i==0){
					cal.add(Calendar.MONTH,0);
				}else{
					cal.add(Calendar.MONTH,1);
				}
				Date dt1=cal.getTime();
				String reStr=sdf.format(dt1);
				str.add(reStr);
			}else if(type.equals("3")){
				if(i==0){
					cal.add(Calendar.YEAR, 0);
				}else{
					cal.add(Calendar.YEAR, 1);
				}
				Date dt1=cal.getTime();
				String reStr=sdf.format(dt1);
				str.add(reStr);
			}
		}
		return str;
	}

	// 获取指定日期指定天数后日期 如：2012-02-28的明天是哪一天
	public static String getDayAfter(String specifiedDay, int p_day) {
		if (p_day == 0)
			return specifiedDay.substring(0, 10);
		Calendar c = Calendar.getInstance();
		Date date = null;
		String dateAfter = null;
		try {
			date = new SimpleDateFormat("yyyy-MM-dd").parse(specifiedDay);
			c.setTime(date);
			int day = c.get(Calendar.DATE);
			c.set(Calendar.DATE, day + p_day);
			dateAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateAfter;
	}

	// 获取指定日期指定天数后日期 如：2012-02-28的明天是哪一天
	public static String getDayAfter(int p_day) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		String dateAfter = null;
		try {
			date = new java.util.Date();
			c.setTime(date);
			int day = c.get(Calendar.DATE);
			c.set(Calendar.DATE, day + p_day);
			dateAfter = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateAfter;
	}
	public static String dateToString(Date value) {
		if(value==null){
			return null;
		}
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time = simple.format(value);
		return time;
	}


	public static String stringFormat(String date, String fromformat,String toformat){
		String  predate=null;
		try{
			Date  pre=  DateUtil.stringToDate(date,fromformat);
			predate=DateUtil.dateToString(pre,toformat);
			
		}catch (Exception e) {
			e.printStackTrace();
		}
		return predate;


	}
}
